﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hybrid;
using System.Drawing;
using System.Windows.Forms;
using System.Media;

namespace Hybrid
{
    class PlayerShip
    {
        private Sprite privateSpriteObject;
        private int privateScore;
        private KeySet privateKeySet;
        private Timer privateProjectileTimeout = new Timer();
        private int privateOwnerID;
        private List<Projectile> privateProjectiles = new List<Projectile>();
        private List<Image> privateProjectileImage;
        private Label privateTitleLabel;
        private ProgressBar privateHealthBar;
        private Label privateScoreLabel;
        private SoundEffects SoundEffect = new SoundEffects();
        private int privateOriginalX;
        private int privateOriginalY;

        //For PowerUp
        private int privateSpeed = 15;
        private int privateScoreInc;
        private int privateDamage = 0;
        private int privateHealth = 100;

        public void ApplyPowerUp(List<int> Values)
        {
            privateSpeed += Values[0];
            privateProjectileTimeout.Interval -= Values[1];
            privateScoreInc += Values[2];
            privateDamage += Values[3];
            privateHealth += Values[4];
        }

        public void Reset()
        {
            privateSpeed = 15;
            privateProjectileTimeout.Interval = 250;
            privateScoreInc = 0;
            privateDamage = 0;
            privateHealth = 100;
            privateSpriteObject.Reset();
            privateSpriteObject.X = privateOriginalX;
            privateSpriteObject.Y = privateOriginalY;

        }


        public PlayerShip(List<Image> SpriteImage, KeySet KeySet, int PlayerAmount, int PlayerIndex, int ownerID, List<Image> projectileImage, Label titleLabel, ProgressBar healthBar, Label scoreLabel)
        {
            int xPos = (730 / (PlayerAmount + 1)) * (PlayerIndex + 1) - ((730 / (PlayerAmount + 1)) / 2);
            privateSpriteObject = new Sprite(SpriteImage, xPos, 570, 0, 0, true, true);
            privateKeySet = KeySet;
            privateProjectileTimeout.Interval = 250;
            privateProjectileTimeout.Tick += new EventHandler(privateProjectileTimeout_Tick);
            privateOwnerID = ownerID;
            privateProjectileImage = projectileImage;
            privateTitleLabel = titleLabel;
            privateTitleLabel.Text = "Player " + (PlayerIndex + 1);
            privateTitleLabel.Location = new Point(xPos - privateTitleLabel.Size.Width / 2, 10);
            privateTitleLabel.Visible = true;
            privateHealthBar = healthBar;
            privateHealthBar.Location = new Point(xPos - privateHealthBar.Size.Width / 2, 30);
            privateHealthBar.Visible = true;
            privateScoreLabel = scoreLabel;
            privateScoreLabel.Text = "Points: 000000";
            privateScoreLabel.Location = new Point(xPos - privateScoreLabel.Size.Width / 2, 45);
            privateScoreLabel.Visible = true;

            privateOriginalX = privateSpriteObject.X;
            privateOriginalY = privateSpriteObject.Y;

        }

        void privateProjectileTimeout_Tick(object sender, EventArgs e)
        {
            privateProjectileTimeout.Stop();
        }

        public Sprite Object(bool ignoreKeys)
        {

            if (!ignoreKeys)
            {
                for (int i = 0; i < privateKeySet.Bools.Count; i++)
                {
                    if (privateKeySet.Bools[i])
                    {
                        if (i == 0 && privateSpriteObject.InBounds(0)) privateSpriteObject.X -= privateSpeed;
                        else if (i == 1 && privateSpriteObject.InBounds(1)) privateSpriteObject.X += privateSpeed;
                        else if (i == 2 && privateSpriteObject.InBounds(2)) privateSpriteObject.Y -= privateSpeed;
                        else if (i == 3 && privateSpriteObject.InBounds(3)) privateSpriteObject.Y += privateSpeed;
                        else if (i == 4 && !privateProjectileTimeout.Enabled)
                        {
                            SoundEffect.ProjectileFire();
                            privateProjectileTimeout.Start();
                            privateProjectiles.Add(new Projectile(privateProjectileImage, privateSpriteObject.X + (privateSpriteObject.Width / 2), privateSpriteObject.Y, -20));
                        }
                    }
                }
            }
            return privateSpriteObject;

        }

        public void DamageShip(int DamageAmount)
        {
            privateHealth -= DamageAmount - privateDamage;
            if (privateHealth <= 0)
            {
                SoundEffect.PlayerShipExplosion();
                privateSpriteObject.StartDestroy();
                KillKeys();
                privateHealthBar.Value = 0;
            }
            else privateHealthBar.Value = privateHealth;
        }

        public int Score
        {
            get
            {
                return privateScore;
            }
            set
            {
                privateScore = value + privateScoreInc;
                privateScoreLabel.Text = "Points: ";
                privateScoreLabel.Text += "000000".Remove(6 - privateScore.ToString().Length, privateScore.ToString().Length);
                privateScoreLabel.Text += privateScore.ToString();
            }
        }

        public void KeyRunThrough(Keys KeyCode, bool state)
        {
            if (!privateSpriteObject.DestroyStarted)
            {
                for (int i = 0; i < privateKeySet.GetKeySet.Count; i++)
                {
                    if (KeyCode == privateKeySet.GetKeySet[i])
                    {
                        privateKeySet.Bools[i] = state;
                    }
                }
            }

        }

        public void KillKeys()
        {
            for (int i = 0; i < privateKeySet.GetKeySet.Count; i++)
            {
                privateKeySet.Bools[i] = false;
            }
        }

        public List<Projectile> Projectiles
        {
            get
            {
                return privateProjectiles;
            }
        }

    }
}
